package su.choco.queuing.service;


import org.springframework.stereotype.Service;
import su.choco.queuing.domain.Queue;
import su.choco.queuing.service.I.QueueService;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class QueueServceImpl implements QueueService {

    private ConcurrentHashMap<String, Queue> queues;

    public QueueServceImpl(){
        queues = new ConcurrentHashMap<String, Queue>();
    }

    @Override
    public Queue get(String name) {
        return queues.get(name);
    }

    @Override
    public boolean add(Queue queue) {
        queues.put(queue.getName(), queue);
        return true;
    }

    @Override
    public boolean remove(String name) {
        queues.remove(name);

        return true;
    }

    @Override
    public boolean clearAll() {
        queues.clear();

        return true;
    }

    @Override
    public ArrayList<Queue> getAll() {
        if(queues.size() == 0){
            return new ArrayList<Queue>();
        }else{
            return new ArrayList<Queue>(queues.values());
        }
    }

    @Override
    public void update(Queue queue) {
        queues.remove(queue.getName());
        add(queue);
    }
}
